{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# requests"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##  实例引入"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "response = requests.get('https://www.baidu.com/')\n",
    "print(type(response))\n",
    "print(response.status_code)\n",
    "print(type(response.text))\n",
    "print(response.text)\n",
    "print(response.cookies)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## get请求"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'bytes'> {'args': {'age': '22', 'name': 'zhou'}, 'headers': {'Accept': '*/*', 'Accept-Encoding': 'gzip, deflate', 'Connection': 'close', 'Host': 'httpbin.org', 'User-Agent': 'python-requests/2.18.4'}, 'origin': '171.217.40.92', 'url': 'http://httpbin.org/get?name=zhou&age=22'}\n",
      "{'Connection': 'keep-alive', 'Server': 'gunicorn/19.8.1', 'Date': 'Sat, 26 May 2018 04:26:10 GMT', 'Content-Type': 'application/json', 'Content-Length': '250', 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Credentials': 'true', 'Via': '1.1 vegur'}\n",
      "<RequestsCookieJar[]>\n"
     ]
    }
   ],
   "source": [
    "import requests\n",
    "response = requests.get('http://httpbin.org/get?name=zhou&age=22')\n",
    "print(type(response.content), response.json())\n",
    "print(response.headers)\n",
    "print(response.cookies)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 获取cookie"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<RequestsCookieJar[<Cookie BAIDUID=3A52CCCDDF19D7680AB4FCCFB6ED01E8:FG=1 for .hao123.com/>, <Cookie hz=0 for .www.hao123.com/>, <Cookie ft=1 for www.hao123.com/>, <Cookie v_pg=normal for www.hao123.com/>]>\n",
      "BAIDUID=3A52CCCDDF19D7680AB4FCCFB6ED01E8:FG=1\n",
      "hz=0\n",
      "ft=1\n",
      "v_pg=normal\n"
     ]
    }
   ],
   "source": [
    "import requests\n",
    "r = requests.get('http://www.hao123.com/')\n",
    "if r.status_code == 200:\n",
    "    print(r.cookies)\n",
    "    for k, v in r.cookies.items():\n",
    "        print(k+ '=' +v)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 会话维持"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\"cookies\":{\"number\":\"123456789\"}}\n",
      "\n"
     ]
    }
   ],
   "source": [
    "import requests\n",
    "s = requests.Session()\n",
    "s.get(\"http://httpbin.org/cookies/set/number/123456789\")\n",
    "r = s.get(\"http://httpbin.org/cookies\")\n",
    "print(r.text)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 验证证书"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "200\n"
     ]
    }
   ],
   "source": [
    "import requests\n",
    "from requests.packages import urllib3 # 导入 包urllib3\n",
    "urllib3.disable_warnings() # 模拟警告信息，就可以把警告信息消除掉\n",
    "url = 'https://www.12306.cn/'\n",
    "r = requests.get(url, verify=False)\n",
    "print(r.status_code)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 认证设置"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (<ipython-input-42-9261455bc8e0>, line 4)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"<ipython-input-42-9261455bc8e0>\"\u001b[1;36m, line \u001b[1;32m4\u001b[0m\n\u001b[1;33m    r = requests.get('http://120.27.34.24:9001' auth=auto)\u001b[0m\n\u001b[1;37m                                                   ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    "import requests\n",
    "from requests.auth import HTTPBasicAuth\n",
    "auth = HTTPBasicAuth('user','123')\n",
    "r = requests.get('http://120.27.34.24:9001' auth=auto)\n",
    "print(r.status_doce)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 异常处理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "HTTPConnectionPool(host='httpbin.org', port=80): Max retries exceeded with url: /get (Caused by ConnectTimeoutError(<urllib3.connection.HTTPConnection object at 0x01430030>, 'Connection to httpbin.org timed out. (connect timeout=0.2)')) request error!\n"
     ]
    }
   ],
   "source": [
    "import requests\n",
    "from requests.exceptions import ReadTimeout,HTTPError, RequestException\n",
    "try:\n",
    "    r = requests.get('http://httpbin.org/get', timeout=0.2)\n",
    "    print(r.status_code)\n",
    "except ReadTimeout:\n",
    "    print('Time out')\n",
    "except HTTPError:\n",
    "    print('Http error!')\n",
    "except RequestException as e:\n",
    "    print(e)\n",
    "    print('request error!')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
